package com.otitan.la.forest.industry.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.la.forest.industry.constant.DicTypeEnum;
import com.otitan.la.forest.industry.mapper.AuditRecordsMapper;
import com.otitan.la.forest.industry.entity.AuditRecordsEntity;
import com.otitan.la.forest.industry.dto.AuditRecordsDTO;
import com.otitan.la.forest.industry.utils.CommonUtils;
import com.otitan.la.forest.industry.vo.AuditRecordsVO;
import com.otitan.la.forest.industry.service.AuditRecordsService;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AuditRecordsServiceImpl extends ServiceImpl<AuditRecordsMapper, AuditRecordsEntity> implements AuditRecordsService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Value("${redis.key-prefix}")
    private String redisKeyPrefix;


    @Override
    public DataResult add(AuditRecordsEntity auditRecords, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.save(auditRecords);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除审核记录】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(AuditRecordsEntity auditRecords, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.updateById(auditRecords);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(long page, long size, AuditRecordsDTO auditRecords) {
        Page pagePlug = new Page(page, size);
        LambdaQueryWrapper<AuditRecordsEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if (StrUtil.isNotEmpty(auditRecords.getDataId())){
            queryWrapper.eq(AuditRecordsEntity::getDataId, auditRecords.getDataId());
        } else {
            return DataResult.fail("业务ID不能为空");
        }
        queryWrapper.orderByAsc(AuditRecordsEntity::getCreateDate);

        IPage<AuditRecordsEntity> iPage = this.page(pagePlug, queryWrapper);
        IPage<AuditRecordsVO> iPageVO = new Page<>();
        BeanUtils.copyProperties(iPage, iPageVO);
        List<AuditRecordsVO> listVO = new ArrayList<>();
        // 转换使用字典的属性
        if (CollUtil.isNotEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(item -> {
                AuditRecordsVO vo = new AuditRecordsVO();
                BeanUtils.copyProperties(item, vo);
                // 需要转换的具体字典属性
                String statusName = CommonUtils.getDicName(redisTemplate, redisKeyPrefix,
                        DicTypeEnum.OUTPUT_STATUS.getValue(), vo.getStatus());
                vo.setStatusName(statusName);
                listVO.add(vo);
            });
        }
        iPageVO.setRecords(listVO);

        return DataResult.success(iPageVO);
    }

    @Override
    public DataResult findOne(String id) {
        AuditRecordsEntity entity = this.getById(id);
        AuditRecordsVO vo = new AuditRecordsVO();
        BeanUtils.copyProperties(entity, vo);
        // 需要转换的具体字典属性

        return DataResult.success(vo);
    }
}